import api from '../../api';
import { message } from 'antd';
import { isZiGuanEvir } from '../../utils/commonFunc';

export default {
  namespace: 'availableAssetM',
  state: {},
  effects: {
    //根据portfolio（组合id）查询资金可用信息
    *getFund({ payload, callback }, { call, put }) {
      let { nodeId, maatApi, hkType } = payload;
      let args = nodeId ? { nodeId: nodeId } : {};
      let response;
      let withMAAT = false;
      if (maatApi) {
        // 判断当前环境是不是资管
        let temp = isZiGuanEvir();
        if (!!temp && temp['iv-user']) withMAAT = true;
      }
      if (withMAAT) {
        // 资管
        response = yield call(api.etfOrderApi.getFundWithMAAT, args);
      } else {
        // maat
        if (hkType == '2') {
          // 如果入参有hkType='2',则为港股通模式
          let arg = nodeId
            ? {
                nodeId: nodeId,
                hkType: '2',
              }
            : {
                hkType: '2',
              };
          response = yield call(api.etfOrderApi.getFund, arg);
        } else if (hkType == '1') {
          // 默认模式
          let arg = nodeId ? { nodeId: nodeId } : {};
          response = yield call(api.etfOrderApi.getFund, arg);
        } else if (hkType == '3') {
          // 股指期货指令
          let arg = nodeId ? { portfolio: nodeId } : {};
          response = yield call(api.etfOrderApi.getFundStockIndex, arg);
          if (response && response.code == '0') {
            callback(response.data);
          } else {
            callback();
            message.error(response && response.msg ? response.msg : '查询资金信息失败!');
          }
          return;
        }
      }
      if (response && response.success) {
        callback(response.data);
      } else {
        callback();
        message.error(response && response.msg ? response.msg : '查询资金信息失败!');
      }
    },
    //根据productGroupId（产品组id）查询资金可用信息
    *getFundByProductGroupId({ payload, callback }, { call, put }) {
      let response = yield call(api.etfOrderApi.getFundByProductGroupId, payload);
      if (response && response.success == true) {
        callback(response.data);
      } else {
        callback();
        message.error(`查询资金信息失败，失败原因：${response?.message}`);
        console.error(
          `查询资金信息失败，失败原因：${response?.message}`,
          '接口名称： queryMultipleByCodeList 接口：inventoryManagement-server/api/v2/inventory/management/financing/queryMultipleByCodeList',
        );
      }
    },
    //获取资金可用 新接口
    *queryAvailable({ payload, callback }, { call, put }) {
      let response;
      const {hkType } = payload;
      delete payload.hkType;
      if (hkType == '2') {
        // 如果入参有hkType='2',则为港股通模式
        // let arg = nodeId
        //   ? {
        //       nodeId: nodeId,
        //       hkType: '2',
        //     }
        //   : {
        //       hkType: '2',
        //     };
        // response = yield call(api.queryAvailable, arg);
      } else if (hkType == '1') {
        // 默认模式
        response = yield call(api.queryAvailableFund, payload);
      } else if (hkType == '3') {
        // 股指期货指令
        // let arg = nodeId ? { portfolio: nodeId } : {};
        // response = yield call(api.etfOrderApi.getFundStockIndex, arg);
        // if (response && response.code == '0') {
        //   callback(response.data);
        // } else {
        //   callback();
        //   message.error(response && response.msg ? response.msg : '查询资金信息失败!');
        // }
        // return;
      }
      if (response && response.code==0) {
        callback(response.data);
      } else {
        callback();
        message.error(response?.msg || '查询资金信息失败!');
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
